package es.android.lottoscan;

import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

/**
 * Pinta el boleto en una view
 * 
 */
public class LottoView extends ImageView {

	static final String TAG = "LottoView";
	
	// Bitmaps estáticos para ahorrar memoria
	static Paint bitmapPaint, textPaint;
	static Bitmap lottoBitmap;

	static final int BITMAP_WIDTH = 500;
	static final int BITMAP_HEIGHT = 260;
	
	public LottoView(Context context, AttributeSet attributes) {
		super(context, attributes);

		if (lottoBitmap == null) {
			lottoBitmap = Bitmap.createBitmap(BITMAP_WIDTH, BITMAP_HEIGHT, Bitmap.Config.RGB_565);
	
			bitmapPaint = new Paint();
			bitmapPaint.setFlags(Paint.FILTER_BITMAP_FLAG);

			textPaint = new Paint();
			textPaint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.SUBPIXEL_TEXT_FLAG);
			textPaint.setColor(Color.BLACK);
			textPaint.setTypeface(Typeface.MONOSPACE);
		}
		
		setData(null);
	}
	
	public void setData(LottoData data) {
		Log.d(TAG, "Setting LottoView Data");
		lottoBitmap.eraseColor(Color.BLACK);
		Canvas canvas = new Canvas(lottoBitmap);
		Drawable drawable = getContext().getApplicationContext().getResources().getDrawable(R.drawable.lotto_empty);
		drawable.setBounds(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
		drawable.draw(canvas);
		canvas.drawBitmap(lottoBitmap, 0, 0, bitmapPaint);
		
		if (data != null && data.getNumber() != null) {
			Log.d(TAG, "Paiting Number " +  data.getNumber());
			// Draws numbers
			textPaint.setTypeface(Typeface.DEFAULT_BOLD);
			textPaint.setTextSize(60);
			textPaint.setTextAlign(Paint.Align.CENTER);
			int spacing = 45;
			
			canvas.drawText(String.valueOf(data.getNumber()).substring(0, 1), 200, 85, textPaint);
			canvas.drawText(String.valueOf(data.getNumber()).substring(1, 2), 200 + spacing, 85, textPaint);
			canvas.drawText(String.valueOf(data.getNumber()).substring(2, 3), 200 + spacing * 2, 85, textPaint);
			canvas.drawText(String.valueOf(data.getNumber()).substring(3, 4), 200 + spacing * 3, 85, textPaint);
			canvas.drawText(String.valueOf(data.getNumber()).substring(4, 5), 200 + spacing * 4, 85, textPaint);
			textPaint.setTextSize(17);
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(data.getDate());
			canvas.drawText(String.valueOf(data.getDateAsNumber()) + "/" + String.valueOf(calendar.get(Calendar.YEAR) % 100) , 450, 33, textPaint);
			textPaint.setTextSize(30);
			canvas.drawText(String.valueOf(data.getSerialNumber())+"ª", 450, 90, textPaint);
			textPaint.setTextSize(25);
			canvas.drawText(String.valueOf(data.getFractionNumber())+"ª", 450, 140, textPaint);
			textPaint.setTextSize(17);
			canvas.drawText(formatDate(data.getDate()), 290, 165, textPaint);
			textPaint.setTextSize(18);
			canvas.drawText(String.valueOf(data.getMysteryNumber()), 370, 205, textPaint);
		} else {
			Log.d(TAG, "Data is null");
		}
		invalidate();
	}

	private String formatDate(Date date) {
		final String months[] = {"Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"};
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH) + " de " + months[calendar.get(Calendar.MONTH)] + " de " + calendar.get(Calendar.YEAR);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {		
		Rect src = new Rect();
		Rect dst = new Rect();

		src.set(0, 0, BITMAP_WIDTH, BITMAP_HEIGHT);
		dst.set(0, 0, getMeasuredWidth(), getMeasuredHeight());
			
		canvas.drawBitmap(lottoBitmap, src, dst, bitmapPaint);		
	}
	
}
